// pages/order-pay/order-pay.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    condition: '待付款', //导航状态
  },
  chat() {
    wx.navigateTo({
      url: '/pages/customer/customer',
    })
  },

  //点击导航
  navSwitch: function (e) {
    // console.log(e.currentTarget.dataset.index)
    let condition = e.currentTarget.dataset.index;
    console.log(condition)
    let name = getApp().globalData.usersname
    var that = this;
    if (condition == '待付款') {
      wx.request({
        url: 'http://localhost/shopping/order_pay_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrpay: res.data.goodspay
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else if (condition == '待分享') {
      wx.request({
        url: 'http://localhost/shopping/order_share_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrshare: res.data.goodsshare
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else if (condition == '待发货') {
      wx.request({
        url: 'http://localhost/shopping/order_deliver_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrdeliver: res.data.goodsdeliver
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else if (condition == '待收货') {
      wx.request({
        url: 'http://localhost/shopping/order_receiving_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrreceiving: res.data.goodsreceiving
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else if (condition == '待评价') {
      wx.request({
        url: 'http://localhost/shopping/order_evaluate_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrevaluate: res.data.goodsevaluate
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    }
    this.setData({
      condition: condition
    })
  },
  info(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/order-info/order-info?id=' + id + '',
    })
  },
  del(e) {
    var id = e.currentTarget.dataset.id
    var goodsnumber = e.currentTarget.dataset.goodsnumber
    var num = e.currentTarget.dataset.num
    var goodsid = e.currentTarget.dataset.goodsid
    console.log(goodsnumber)
    var that = this;
    wx.showModal({
      title: '',
      content: '是否确认删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: 'http://localhost/shopping/goods_num_del.php',
            header: {
              'Content-Type': 'application/json'
            },
            data: {
              id: goodsid,
              goodsnumber: goodsnumber,
              num: num
            },
            success: function (res) {
              console.log(res.data)

            },
            fail: function (res) {
              console.log("fail");
            }
          })
          wx.request({
            url: 'http://localhost/shopping/admin_order_del.php',
            header: {
              'Content-Type': 'application/json'
            },
            data: {
              id: id
            },
            success: function (res) {
              // console.log(res.data)
              that.Order();
              wx.showToast({
                title: '删除订单成功',
                icon: 'success',
                duration: 2000
              })
              that.setData({})
            },
            fail: function (res) {
              console.log("fail");
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  pay(e) {
    var id = e.currentTarget.dataset.id
    var time = util.formatTime(new Date());
    wx.showModal({
      title: '支付',
      content: '是否进行付款',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          console.log('确认付款')
          wx.request({
            url: 'http://localhost/shopping/order_pay_edit.php',
            header: {
              'Content-Type': 'application/json'
            },
            data: {
              startdate: time,
              id: id
            },
            success: function (res) {
              // console.log(res.data)
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 2000
              })
            },
            fail: function (res) {
              console.log("fail");
            }
          })
        } else { //这里是点击了取消以后
          console.log('取消')
        }
      }
    })

  },
  Order: function () {
    var that = this;
    var condition = that.data.condition
    let name = getApp().globalData.usersname

    console.log(condition)
    if (condition == '待付款') {
      wx.request({
        url: 'http://localhost/shopping/order_pay_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrpay: res.data.goodspay
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else if (condition == '待分享') {
      wx.request({
        url: 'http://localhost/shopping/order_share_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrshare: res.data.goodsshare
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else if (condition == '待发货') {
      wx.request({
        url: 'http://localhost/shopping/order_deliver_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrdeliver: res.data.goodsdeliver
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else if (condition == '待收货') {
      wx.request({
        url: 'http://localhost/shopping/order_receiving_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrreceiving: res.data.goodsreceiving
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else if (condition == '待评价') {
      wx.request({
        url: 'http://localhost/shopping/order_evaluate_search.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          name: name
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            arrevaluate: res.data.goodsevaluate
          })
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    }



  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var condition = e.condition
    var that = this


    that.setData({
      condition: condition
    })
    that.Order();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})